<?php

namespace App\Service;

use App\Model\ConfigHouseModel;
use App\Model\NoticeModel;
use App\Model\SystemNoticeModel;
use App\Model\UserHouseModel;
use App\Utility\DataConversion;
use App\WebSocketController\Index;
use EasySwoole\Actor\Exception\InvalidActor;
use EasySwoole\ORM\Exception\Exception;
use Throwable;

class NoticeService extends BaseService
{
    /**
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getNotice($type): array
    {
        $notices = NoticeModel::create()->where(['is_deleted' => 0, 'type' => $type])->order('id', "DESC")->all();
        $data = [];

        foreach ($notices as $notice) {
            $data[] = $notice['title'];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $type
     * @param $limit
     * @param $page
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getNoticeList($type, $limit, $page): array
    {
        $model = NoticeModel::create()->where(['is_deleted' => 0, 'type' => $type]);
        $model->limit($limit * ($page - 1), $limit)->withTotalCount();
        $notices = $model->order('id', "DESC")->all();
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();
        $data = [];

        foreach ($notices as $notice) {
            $data[] = [
                'id' => $notice['id'],
                'content' => $notice['title'],
                'time' => DataConversion::getInstance()->timeToDate($notice['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getNoticeDetail($id): array
    {
        $notice = NoticeModel::create()->getChat(['id' => $id]);
        if (!$notice) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $notice->toArray()
        ];
    }

    /**
     * @param $uid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getDelivery($uid): array
    {
        $houses = UserHouseModel::create()->where(['uid' => $uid, 'status' => 1])->all();

        if (empty($houses)) {
            return [
                'result' => false,
                'msg' => '暂未绑定房屋'
            ];
        }

        $data = [];

        foreach ($houses as $house) {
            $time = ConfigHouseModel::create()->getConfig(['id' => $house['unit']]);
            $h = ConfigService::getInstance()->getHouse($house['village'], $house['unit'], $house['floor'], $house['house']);
            $data[] = [
                'name' => $house['name'],
                'house' => $h,
                'time' => $time
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getDeliveryNotice(): array
    {
        $content = NoticeModel::create()->getChat(['title' => '交房须知']);
        if (!$content) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $content->toArray()
        ];
    }

    /**
     * 发送系统消息
     * @throws Exception
     * @throws InvalidActor
     * @throws Throwable
     */
    public function sendNotice($uid, $title, $icon, $content, $type)
    {
        $param = [
            'sid' => $uid,
            'title' => $title,
            'icon' => $icon,
            'content' => $content,
            'type' => $type
        ];

        SystemNoticeModel::create()->addChat($param);
        foreach ($uid as $item) {
            $message = [
                'sid' => $item,
                'type' => 'user',
                'msg' => [
                    'userinfo' => [
                        'sid' => 0,
                    ],
                    'type' => 'text',
                    'content' => [
                        'typer' => $type,
                        "text" => "【" . $title . "】" . $content
                    ],
                    'time' => DataConversion::getInstance()->timeToDate(time())
                ]
            ];

            $chat = new Index();
            $chat->sendSys($message);
        }

    }
}